package com.education.welco.controller;

import com.education.common.pojo.AjaxResult;
import com.education.welco.pojo.SysAppVersion;
import com.education.welco.service.SysAppVersionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/sysAppVersion")
public class SysAppVersionController {

    @Autowired
    private SysAppVersionService sysAppVersionService;

    //@Value("${apkUrl}")
    private String apkUrl;

    /*@Value("${apkHtmlUrl}")
    private String apkHtmlUrl;*/

    @PostMapping(value = "/addAppVersion")
    @ApiOperation("添加版本信息")
    public AjaxResult addAppVersion(@RequestBody SysAppVersion sysAppVersion) {
        return sysAppVersionService.addAppVersion(sysAppVersion);
    }

    @PostMapping(value = "/delAppVersion")
    @ApiOperation("删除版本信息")
    public AjaxResult delAppVersion(@RequestBody SysAppVersion sysAppVersion) {
        return sysAppVersionService.delAppVersion(sysAppVersion);
    }

    @PostMapping(value = "/appVersionList")
    @ApiOperation("版本信息列表")
    public AjaxResult appVersionList(@RequestBody SysAppVersion sysAppVersion) {
        return sysAppVersionService.appVersionList(sysAppVersion);
    }

    @PostMapping(value = "/appDownLoad")
    @ApiOperation("app下载地址")
    public void appDownLoad(@RequestBody SysAppVersion sysAppVersion, HttpServletResponse response) throws IOException {
        QiniuUtil.downLoadFile(sysAppVersion.getApkUrl(), response);
    }

    @PostMapping(value = "/fileUpload")
    @ApiOperation("上传apk(本地)")
    private AjaxResult fileUpload(@RequestParam("file") MultipartFile file, @RequestParam("version") String version) {
        String url = version + "/" + file.getOriginalFilename();
        String path = apkUrl + url;
        File newFile = new File(path);
        if (!newFile.getParentFile().exists()) {
            try {
                newFile.getParentFile().mkdirs();
                newFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            file.transferTo(newFile);
            return AjaxResult.success().put("data", url);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败");
        }
    }

    /**
     * app 获取最新版本信息
     */
    @GetMapping("/getLatSysAppVersion")
    public AjaxResult getLatSysAppVersion() {
        SysAppVersion appVersion = sysAppVersionService.getLatSysAppVersion();
        return AjaxResult.success(appVersion);
    }

    /*@RequestMapping("/getQRcode")
    public void getQRcode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysAppVersion appVersion = sysAppVersionService.getLatSysAppVersion();
        BufferedImage bufferedImage =
                QrCodeUtil.generate(apkHtmlUrl, 500, 500);
        ServletOutputStream outputStream = response.getOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //二维码中包含的信息
        //String content = "姓名:十二余\n博客：https://www.cnblogs.com/jing5464";
        //Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        //// 指定编码格式
        //hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //// 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
        //hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //// 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
        //BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
        ////设置请求头
        //response.setHeader("Content-Type","application/octet-stream");
        //response.setHeader("Content-Disposition", "attachment;filename=" + "二维码.png");
        //OutputStream outputStream = response.getOutputStream();
        //MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
        //outputStream.flush();
        //outputStream.close();
    }*/
}
